package client.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import client.ClientController;
import shared.Consts;
import packet.ChatReq;

/**
 * A chat widget in the form of a JPanel
 * @author Joel Garboden
 */
public class ChatComponent extends JPanel
{
  private ClientController controller;
  private JTextArea chatLog;
  private JScrollPane chatPane;
  private JTextField message;
  private JButton send;

  /**
   * Constructor<br/>
   * Initiates the chat component and links to it's controller
   * @param controller client's controller
   * @param size preferred size of the chat widget
   */
  public ChatComponent(ClientController controller, Dimension size)
  {
    this.controller = controller;

    setPreferredSize(size);
    
    setLayout(new BorderLayout());
    chatLog = new JTextArea();
    chatPane = new JScrollPane(chatLog);
    chatLog.setEditable(false);
   
    message = new JTextField(Consts.DEFAULT_CHAT_PROMPT);

    message.setMaximumSize(new Dimension(2000, 20));
    message.setEditable(false);

    send = new JButton("Send");
    send.setEnabled(false);

    Box messagingBox = Box.createHorizontalBox();
    add(chatPane, BorderLayout.CENTER);
    messagingBox.add(message);
    messagingBox.add(send);
    add(messagingBox, BorderLayout.SOUTH);

    TextSelector.install();

    send.addActionListener(new java.awt.event.ActionListener()
    {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        sendActionPerformed(evt);
      }
    });

    message.addActionListener(new java.awt.event.ActionListener()
    {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        sendActionPerformed(evt);
      }
    });
    
    setEnabled(false);
  }

  /**
   * Action method for the sending of a chat message
   * @param evt (unused) required for ActionListener
   */
  private void sendActionPerformed(java.awt.event.ActionEvent evt)
  {
    if(!message.getText().equals(""))
    {
      controller.sendRequest(new ChatReq(controller.playerName, message.getText()));
      message.setText("");
      
      SwingUtilities.invokeLater(new Runnable()
      {
        @Override
        public void run()
        {
          chatLog.setCaretPosition(chatLog.getText().length());
        }
      });
    }
  }

  /**
   * Posts a formatted chat message to the chat log
   * @param name posting player's name (or System)
   * @param chatMsg the message to be displayed
   */
  public void postChat(String name, String chatMsg)
  {
    chatLog.append('\n' + name + ": " + chatMsg);
  }

  /**
   * Toggles the enabling of chat
   * @param enabled whether chat is enabled or not
   */
  public void enableChat(boolean enabled)
  {
    message.setEditable(enabled);
    send.setEnabled(enabled);
    setEnabled(enabled);
  }
}
